package reflectx

import (
	"gitee.com/zhongguo168a/gocodes/datax/schemax"
)

type FieldRef struct {
}

func (f *FieldRef) RefMapGet(target IRefObject, field string, key string) (val interface{}, isNil bool) {
	obj := target.(*schemax.Field)
	switch field {
	case "Tags":
		ival, ok := obj.Tags[key]
		return ival, ok
	}
	return nil, true
}

func (f *FieldRef) RefMapSet(target IRefObject, field string, key string, val interface{}) {
	obj := target.(*schemax.Field)
	switch field {
	case "Tags":
		if obj.Tags == nil {
			obj.Tags = map[string]string{}
		}
		obj.Tags[key] = val.(string)
	}
}

func (f *FieldRef) RefMapKeys(target IRefObject, field string) (r []string) {
	obj := target.(*schemax.Field)
	switch field {
	case "Tags":
		for key := range obj.Tags {
			r = append(r, key)
		}
	}
	return
}

func (f *FieldRef) RefMapNew(field string) (r interface{}) {
	switch field {
	case "Tags":
		r = make(map[string]string)
	}
	return
}

func (f *FieldRef) RefSliceGet(target IRefObject, field string, index int) (val interface{}, isNil bool) {
	obj := target.(*schemax.Field)
	switch field {
	case "Comments":
		val := obj.Comments[index]
		return val, false
	}
	return nil, true
}

func (f *FieldRef) RefSliceSet(target IRefObject, field string, index int, val interface{}) {
	obj := target.(*schemax.Field)
	switch field {
	case "Comments":
		obj.Comments[index] = val.(string)
	}
}

func (f *FieldRef) RefSliceNew(field string, len int, cap int) (r interface{}) {
	switch field {
	case "Comments":
		r = make([]string, len, cap)
	}
	return
}

func (f *FieldRef) RefSliceLength(target IRefObject, field string) (r int) {
	obj := target.(*schemax.Field)
	switch field {
	case "Comments":
		r = len(obj.Comments)
	}
	return
}

func (f *FieldRef) RefHas(field string) bool {
	switch field {
	case "Name":
		return true
	case "Type":
		return true
	case "Value":
		return true
	}
	return false
}

func (f *FieldRef) RefGet(target IRefObject, field string) (interface{}, bool) {
	obj := target.(*schemax.Field)
	switch field {
	case "Name":
		return obj.Name, false
	case "Type":
		val := obj.Type
		return val, val == nil
	case "Value":
		return obj.Value, false
	case "Tags":
		return obj.Tags, obj.Tags == nil
	}
	return nil, true
}

func (f *FieldRef) RefSet(target IRefObject, field string, val interface{}) {
	obj := target.(*schemax.Field)
	switch field {
	case "Name":
		obj.Name = val.(string)
	case "Type":
		obj.Type = val.(schemax.IType)
	case "Value":
		obj.Value = val.(string)
	}
	return
}

func (f *FieldRef) RefNew() IRefObject {
	return &schemax.Field{}
}

func (f *FieldRef) RefType() string {
	return "field"
}
